package com.woniuxy.demo.epai.controller;


import com.woniuxy.demo.epai.common.Result;
import com.woniuxy.demo.epai.controller.form.ProductRecordForm;
import com.woniuxy.demo.epai.model.Product;
import com.woniuxy.demo.epai.model.ProductRecord;
import com.woniuxy.demo.epai.service.IProductRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 拍卖记录表 前端控制器
 * </p>
 *
 * @author epai
 * @since 2021-04-12
 */
@RestController
@RequestMapping("/epai/product-record")
@Slf4j
public class ProductRecordController {
    @Autowired
    private IProductRecordService service;

    @PostMapping("/addProductRecord")
    public Result addProductRecord(@RequestBody ProductRecordForm form){
        log.info("进入方法,参数为,{}",form);
        ProductRecord productRecord = new ProductRecord();
        BigDecimal basePrice = new BigDecimal(form.getBasePrice());
        productRecord.setBasePrice(basePrice);
        productRecord.setPid(form.getPid());

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
        productRecord.setStarttime(new Date());
        if(form.getMode().equals("会场模式")){
            Date deadlineTime = new Date(new Date().getTime()+60*5*1000);
            productRecord.setDeadline(deadlineTime);
        }else if(form.getMode().equals("秒杀")){
            Date deadlineTime = new Date(new Date().getTime()+60*4*1000);
            productRecord.setDeadline(deadlineTime);
        }
        productRecord.setStatus(1);
        productRecord.setHot(0);
        productRecord.setProAucId(1);
        BigDecimal TransactionPrice = new BigDecimal("0");
        productRecord.setTransactionPrice(TransactionPrice);
        BigDecimal boodPrice = new BigDecimal("0");
        productRecord.setBond(boodPrice);
        productRecord.setStreamingTime(0);
        productRecord.setMode(form.getMode());
        log.info("进入方法,参数为,{}",productRecord);
        service.addProductRecord(productRecord);
        return Result.success();
    }

    @PutMapping("/updProductRecord")
    public Result updProductRecord(@RequestBody ProductRecordForm form){
        log.info("进入方法,参数为,{}",form);
        ProductRecord productRecord = new ProductRecord();
        BigDecimal basePrice = new BigDecimal(form.getBasePrice());
        productRecord.setBasePrice(basePrice);
        productRecord.setPid(form.getPid());

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
        productRecord.setStarttime(new Date());
        if(form.getMode().equals("会场模式")){
            Date deadlineTime = new Date(new Date().getTime()+60*60*5);
            productRecord.setDeadline(deadlineTime);
        }else if(form.getMode().equals("秒杀")){
            Date deadlineTime = new Date(new Date().getTime()+60*60*10);
            productRecord.setDeadline(deadlineTime);
        }
        productRecord.setStatus(3);
        productRecord.setHot(0);
        productRecord.setProAucId(1);
        BigDecimal TransactionPrice = new BigDecimal("0");
        productRecord.setTransactionPrice(TransactionPrice);
        BigDecimal boodPrice = new BigDecimal("0");
        productRecord.setBond(boodPrice);
        productRecord.setStreamingTime(0);
        productRecord.setMode(form.getMode());
        log.info("进入方法,参数为,{}",productRecord);
        service.updProductRecord(productRecord);
        return Result.success();
    }

    @PutMapping("/addhot")
    public Result addhot(@RequestBody Product product){
        ProductRecord productRecord = new ProductRecord();
        productRecord.setPid(product.getId());
        service.updProductRecord(productRecord);
        return Result.success();
    }
}

